/*
 ============================================================================
 Name        : ircbot.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include "irc.h"
#include "events.h"
//#include "buffer.h"

void (*fp)(ServerInfo *, char *);
Node *root;

int main(int argc, char **argv) {
	ServerInfo irc;
	ClientInfo user;
	IRCMessage *msg;
	char *readbuffer;
	readbuffer = calloc(1, IRCMAXSZ);
	if (readbuffer == NULL) {
		printf("buffer allocation failed!");
		return -1;
	}

	/* node stuff */
	root = calloc(1, sizeof(struct node));
	root->command = "PLZ";
	/* end of node stuff */

	memset(&irc, 0, sizeof(irc));
	memset(&user, 0, sizeof(user));

	user.nick = "ircbot";
	user.ident = "botident";
	user.realname = "Irc Bot";

	irc.host = "localhost";
	irc.port = "6667";

	if (argc == 3) {
		irc.host = argv[1];
		irc.port = argv[2];
	} else if (argc == 2) {
		irc.host = argv[1];
	} else {
		printf("usage: %s host [port]\n", argv[0]);
		return EXIT_SUCCESS;
	}
	/* does this try all resolved addresses in sequence? */
	if (irc_connect(&irc) == -1) {
		/* connection to irc server was not established, quitting .. */
		printf("Failed to connect %s port %s\n", irc.host, irc.port);
		return EXIT_FAILURE;
	}

	if (irc_handler_add(root, "PRIVMSG", &on_privmsg) == 0) {
		printf("handler added\n");
	} else {
		printf("handler addition failed\n");
	}
	if (irc_handler_add(root, "PING", &on_ping) == 0) {
		printf("handler added\n");
	} else {
		printf("handler addition failed\n");
	}

	/* try to register client with irc server
	 * (for now just sendto the socket and not care about return codes)
	 */
	if (irc_register(&irc, &user) == 0) {
		irc.cleanbuf = 0;
		/* these are probably cleared out before */
//		memset(irc.recvbuf, 0, sizeof(irc.recvbuf));
		memset(irc.buffer, 0, sizeof(irc.buffer));
		//memset(ircbuffer, 0, sizeof(ircbuffer));
		while ((msg = irc_getMessage(&irc)) != NULL) {
			irc.lastmsg = msg;
			printf("PREFIX: [%s] CMD: [%s] PARM: [%s]\n", irc.lastmsg->prefix,
					irc.lastmsg->command, irc.lastmsg->params);
			if (strstr(irc.lastmsg->command, "PING") != NULL) {
				fp = irc_handler_get(root, "PING");
				(*fp)(&irc, irc.lastmsg->params);
			}
			free(msg->command);
			free(msg->params);
			free(msg->prefix);
			free(msg);
		}
		/*
		 while (read(irc.socket, readbuffer, IRCMAXSZ) > 0) {
		 // buffer handling needs redesign/rewrite
		 irc.lastmsg = do_buffer(&irc, readbuffer);
		 printf("PREFIX: [%s] CMD: [%s] PARM: [%s]\n", irc.lastmsg->prefix,
		 irc.lastmsg->command, irc.lastmsg->params);
		 if (strstr(irc.lastmsg->command, "PING") != NULL) {
		 fp = irc_handler_get(root, "PING");
		 (*fp)(&irc, irc.lastmsg->params);

		 }

		 free(irc.lastmsg->prefix);
		 free(irc.lastmsg->command);
		 free(irc.lastmsg->params);
		 free(irc.lastmsg);
		 */
//	}
		perror("Connection closed");
	} else {
		printf("Failed to register connection.\n");
	}
	printf("Quitting..\n");
	close(irc.socket);
	freeaddrinfo(irc.res);
	return EXIT_SUCCESS;
}
